﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CDStore3Tiers
{
    public class XuLyLog
    {
        StoreCDDataContext db;
        XuLyForm XL = new XuLyForm();
        public List<Log> list()
        {
            db = new StoreCDDataContext();
            db.Connection.ConnectionString = getConfig.getConnection();
            var a = (from t in db.Logs
                     orderby t.ID descending
                     select t).ToList<Log>();
            return a;
        }
        public List<Log> SearchLog(List<Log> L, string val)
        {
            db = new StoreCDDataContext();
            db.Connection.ConnectionString = getConfig.getConnection();
            val = val.ToLower();
            return L.Where(c => c.ID.ToString().ToLower().Contains(val) ||
                c.Logger.ToString().ToLower().Contains(val) ||
                c.Date.ToString().ToLower().Contains(val) ||
                c.Message.ToString().ToLower().Contains(val)).ToList<Log>();
        }
        public int TongSoTrang(int pagesize)
        {
            db = new StoreCDDataContext();
            db.Connection.ConnectionString = getConfig.getConnection();
            var a = (from t in db.Logs
                     select t).Count();
            if (a % pagesize == 0)
            {
                a = (int)a / pagesize;
            }
            else
            {
                a = ((int)a / pagesize) + 1;
            }
            return a;
        }
        public int SoTrangHienTai(int pagenumber, int pagesize)
        {
            int a = pagenumber;
            if (a % pagesize == 0)
            {
                a = (int)a / pagesize;
            }
            else
            {
                a = ((int)a / pagesize) + 1;
            }
            return a + 1;
        }

        public void ToCsV(DataGridView dGV, string filename)
        {
            string stOutput = "";
            string sHeaders = "";

            for (int j = 0; j < dGV.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            for (int i = 0; i < dGV.RowCount - 1; i++)
            {
                string stLine = "";
                for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
                stOutput += stLine + "\r\n";
            }
            Encoding utf16 = Encoding.GetEncoding(1254);
            byte[] output = utf16.GetBytes(stOutput);
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(output, 0, output.Length);
            bw.Flush();
            bw.Close();
            fs.Close();
        }
    }
}
